package com.masterclient.app.businessmodule.minemodule.point;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.widget.empty.FCPageStateView;
import com.eoner.baselib.widget.navgation.FCNavigationAction;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselib.widget.navgation.IFCNavigationListener;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.managerlibrary.user.UserDataManager;
import com.masterclient.app.R;
import com.masterclient.app.base.CashToastDialog;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.modules.bridge.BridgeActivity;
import com.masterclient.app.businessmodule.minemodule.cash.BinderMobileActivity;
import com.masterclient.app.businessmodule.minemodule.withdraw.GainCashActivity;
import com.eoner.uikit.adapter.simplepage.SimplePagerAdapter;
import com.masterclient.app.businessmodule.usermodule.login.index.QuickLoginActivity;
import com.masterclient.app.businessmodule.minemodule.point.adapter.PointEarnAdapter;
import com.masterclient.app.businessmodule.minemodule.point.contract.PointMainContract;
import com.masterclient.app.businessmodule.minemodule.point.contract.PointMainPresenter;
import com.masterclient.app.businessmodule.minemodule.point.model.PointMainBean;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.widget.EnhanceTabLayout;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/10/11 14:47
 * 功能说明：
 */

@Route(path = AroutePath.POINT_ACTIVITY)
public class PointActivity extends FCBusinessActivity<PointMainPresenter> implements PointMainContract.View {

    @BindView(R.id.tabLayout)
    EnhanceTabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.tv_tixian)
    TextView tvTixian;
    @BindView(R.id.custom_navbar)
    FCNavigationBar customNavigationBar;

    @BindView(R.id.point_main_total)
    TextView pointMainTotal;
    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.point_main_bg)
    ImageView pointMainBg;
    @BindView(R.id.point_main_earn_btn)
    TextView pointMainEarnBtn;
    @BindView(R.id.point_main_recycler)
    RecyclerView pointMainRecycler;
    @BindView(R.id.point_main_head_layout)
    LinearLayout pointMainHeadLayout;
    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.point_main_rule_name)
    TextView pointMainRuleName;
    @BindView(R.id.point_main_rule_icon)
    ImageView pointMainRuleIcon;
    @BindView(R.id.point_rule_ll)
    LinearLayout pointRuleLl;
    @BindView(R.id.point_main_rule_title)
    TextView pointMainRuleTitle;
    @BindView(R.id.point_main_bar)
    FrameLayout pointMainBar;
    @BindView(R.id.rl_bottom)
    RelativeLayout rlBottom;
    @BindView(R.id.point_main_cardview)
    CardView pointMainCardview;
    @BindView(R.id.point_main_validity_layout)
    LinearLayout pointMainValidityLayout;
    @BindView(R.id.point_main_validity_text)
    TextView pointMainValidityText;

    private List<Fragment> fragments;

    private PointFragment pointIncomeFragment;//获取
    private PointFragment pointExpendFragment;//支出
    private PointExchangeFragment pointExchangeFragment;//提现

    private SimplePagerAdapter cashPagerAdapter;
    private PointEarnAdapter earnAdapter ;
    String amount = "0.00";
    String url, integralUrl;
    String sh_is_bind_mobile;
    private String signUrl = "";
    private int statusHeight = 0;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.point;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        statusHeight = ImmersionBar.getStatusBarHeight(this);
        ImmersionBar.with(this).statusBarDarkFont(true).statusBarView(statusBar).transparentStatusBar().init();
        pointMainBg.getLayoutParams().height = ScreenUtils.dp2px(314) + statusHeight;

        Utils.setDin(pointMainTotal, this);

        earnAdapter = new PointEarnAdapter();
        pointMainRecycler.setLayoutManager(new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        pointMainRecycler.setAdapter(earnAdapter);
        earnAdapter.bindToRecyclerView(pointMainRecycler);


        mPresenter.getPointMain();

        fragments = new ArrayList<>();
        pointIncomeFragment = PointFragment.getInstance("1");
        pointExpendFragment = PointFragment.getInstance("2");
        pointExchangeFragment = new PointExchangeFragment();


        fragments.add(pointIncomeFragment);
        fragments.add(pointExpendFragment);
        fragments.add(pointExchangeFragment);

        String[] sTitle = {"金币收入", "金币支出", "兑换记录"};
        cashPagerAdapter = new SimplePagerAdapter(getSupportFragmentManager(), fragments);
        for (int i = 0; i < sTitle.length; i++) {
            tabLayout.addTab(sTitle[i], i == 0);
        }
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(cashPagerAdapter);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout.getTabLayout()));
        tabLayout.setupWithViewPager(viewPager);

        customNavigationBar.setGzgNavigationListener(new IFCNavigationListener() {
            @Override
            public void onNavigationClickListen(int action) {
                if (action == FCNavigationAction.NAV_LEFT_CLICK) {
                    finish();
                }
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 103:
                viewPager.setCurrentItem(2);
                viewPager.postDelayed(() -> {
                    pointExchangeFragment.refreshList();
                }, 1000);

                CashToastDialog cashToastDialog = new CashToastDialog(mContext);
                cashToastDialog.show();

                break;
            case 104:
                Intent intent = new Intent(mContext, GainCashActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("amount", amount);
                bundle.putString("url", url);
                intent.putExtras(bundle);
                startActivityForResult(intent, 103);
                break;
        }
    }

    @OnClick({R.id.rl_back, R.id.tv_tixian, R.id.point_rule_ll, R.id.point_main_earn_btn})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_tixian:
                if ("1".equals(sh_is_bind_mobile)) {
                    Intent intent = new Intent(mContext, GainCashActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("amount", amount);
                    bundle.putString("url", url);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, 103);
                } else {
                    ConfirmDialog confirmDialog = new ConfirmDialog(mContext, "为了您的资金安全，请先绑定手机号", "暂不绑定", "去绑定");
                    confirmDialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
                        @Override
                        public void cancel() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void confirm() {
                            confirmDialog.dismiss();
                            startActivityForResult(new Intent(mContext, BinderMobileActivity.class), 88);
                        }
                    });
                    confirmDialog.show();
                }

                break;
            case R.id.point_main_earn_btn:
                if (UserDataManager.getInstance().isSystemLogin()) {
                    if (TextUtils.isEmpty(signUrl)) return;
                    startActivitry(BridgeActivity.class,new String[][]{{"url",signUrl}});
                } else {
                    startActivity(new Intent(mContext, QuickLoginActivity.class));
                }
            case R.id.point_rule_ll:
                if (!TextUtils.isEmpty(integralUrl)) {
                    startActivitryForResult(BridgeActivity.class, new String[][]{{"url", integralUrl}, {"title", "金币兑换规则"}});
                }
                break;
        }
    }



    @Override
    public void showData(PointMainBean data) {
        integralUrl = data.getIntegral_rule();
        pointMainTotal.setText(data.getIntegral());
        signUrl = data.getSign_url();

        if (!TextUtils.isEmpty(data.getIntegral_expiring())){
            pointMainValidityText.setText(data.getIntegral_expiring());
            pointMainValidityLayout.setVisibility(View.VISIBLE);
        }else {
            pointMainValidityLayout.setVisibility(View.GONE);
        }

        if (data.getNav_list()!=null&&data.getNav_list().size()>0) {
            earnAdapter.setNewData(data.getNav_list());
            pointMainCardview.setVisibility(View.VISIBLE);
        }else {
            pointMainCardview.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClickPageStateErrorView() {
        super.onClickPageStateErrorView();
        mPresenter.getPointMain();

        pointIncomeFragment.refreshList();
        pointExpendFragment.refreshList();
        pointExchangeFragment.refreshList();

    }

    @Override
    public FCPageStateView baseRefreshPageState(int viewState) {
        if (viewState == FCPageViewState.PAGE_STATE_NORMAL) {
            customNavigationBar.setVisibility(View.GONE);
            pointMainBg.setVisibility(View.VISIBLE);
        }
        return super.baseRefreshPageState(viewState);
    }

    @Override
    public FCPageStateView baseRefreshPageState(int viewState, String msg) {
        if (viewState == FCPageViewState.PAGE_STATE_NET_ERROR) {

            FCPageStateView stateView = super.baseRefreshPageState(viewState,msg);
            baseRefreshPageStateTopOffset(ScreenUtils.dp2px(46)+statusHeight);

            customNavigationBar.setTitle("金币").setVisibility(View.VISIBLE);
            pointMainBg.setVisibility(View.INVISIBLE);
            return stateView;
        }
        return super.baseRefreshPageState(viewState,msg);
    }

    @Override
    public void loadFail(String s) {
        ToastUtil.showToast(s);
    }

}
